package other;

import java.util.*;

public class Register {
    public static void main(String[] args) {
        Register register = new Register();
        Scanner scanner = new Scanner(System.in);
        System.out.println("记牌器初始化结束，请输入牌名和已出牌数(1 2):");
        new Thread(
                ()->{
                    while (true) {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (register.isOver()) {
                            register.refresh();
                            System.out.println("重置所有牌数");
                        }
                    }
                }
        ).start();
        while (scanner.hasNext()) {
            String input = scanner.nextLine();
            String[] split = input.split(" ");
            if (split.length == 2) {
                Integer cardKey = Integer.parseInt(split[0]);
                Integer num = Integer.valueOf(split[1]);
                register.record(cardKey, num);
                register.printAllCards();
            } else {
                System.out.println("输入有误，请重新输入");
            }
        }
    }

    private Map<String, Integer> card;
    private Map<Integer, String> cardMapInteger;
    String[] cards = "A,2,3,4,5,6,7,8,9,10,J,Q,K".split(",");

    public Register() {
        if (card == null) {
            card = new HashMap<>(13);
            // A-K
            for (int i = 0; i < cards.length; i++) {
                card.put(cards[i], 0);
            }
        }
        if (cardMapInteger == null) {
            cardMapInteger = new HashMap<>(13);
            // A-K
            for (int i = 1; i <= cards.length; i++) {
                cardMapInteger.put(i, cards[i - 1]);
            }
        }
    }
    //输出所有牌数
    public void printAllCards(){
        card.keySet().stream().forEach(key -> {
            if (card.get(key) == 0) {
                return;
            }
            System.out.println(key + " " + card.get(key));
        });
    }
    // 记牌
    public boolean record(Integer cardkey, int num) {
        if (cardMapInteger.containsKey(cardkey)) {
            if (num < 0 || num > 4 || (card.get(cardMapInteger.get(cardkey)) - num) < 0) {
                System.out.println("输入牌数有误");
                return false;
            }
            String cardk = cardMapInteger.get(cardkey);
            Integer numOld = card.get(cardk);
            card.put(cardk, numOld - num);
            return true;
        } else {
            System.out.println("不存在该牌");
            return false;
        }
    }
    public boolean isOver() {
        // 判断card所有牌是否为0
        String[] keys = card.keySet().toArray(new String[0]);
        int l = 0;
        int r = keys.length - 1;
        while (l < r){
            if(card.get(keys[l]) > 0  || card.get(keys[r]) > 0){
                return false;
            }
            l++;
            r--;
        }
        return true;
    }
    public void refresh() {
        card.keySet().stream().forEach(key -> card.put(key, 4));
    }
}
